{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6dedab0-64bd-44a2-87c7-72df72b0a44c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc7bed37-73bd-4ce0-89dd-9d7f301c73fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = 'data/pricing_data.csv'\n",
    "uploaded_data = pd.read_csv(file_path, encoding='gbk')\n",
    "uploaded_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dca1b6e2-76a7-4132-a2f5-c94ccb10a659",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = uploaded_data[['Price']]\n",
    "y = uploaded_data['Sales']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1725bfb3-0129-4342-8ce5-205fe83b45b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = LinearRegression()\n",
    "model.fit(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55ed0536-d8cf-4866-bc75-cea7da14d3a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "intercept = model.intercept_\n",
    "slope = model.coef_[0]\n",
    "print(f\"截距: {intercept:.2f}, 斜率: {slope:.2f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8889cfca-cf74-41a9-9f34-4e127199baef",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(8, 6))\n",
    "sns.regplot(\n",
    "    x='Price',\n",
    "    y='Sales',\n",
    "    data=uploaded_data,\n",
    "    ci=None,\n",
    "    line_kws={\"color\": \"red\", \"label\": f\"y = {intercept:.2f} + {slope:.2f}x\"}\n",
    ")\n",
    "plt.title('Pricing vs Sales Regression', fontsize=16)\n",
    "plt.xlabel('Price (Yuan)', fontsize=12)\n",
    "plt.ylabel('Sales (Yuan)', fontsize=12)\n",
    "plt.legend()\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
